# 1. 导入模块
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

# 2. 创建浏览器对象
# driver = webdriver.Chrome("./chromedriver")   # 废弃

# Selenium Manager 自动管理驱动 （pip install webdriver-manager）
# 需要翻墙https://chromedriver.storage.googleapis.com
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# 本地驱动
# https://googlechromelabs.github.io/chrome-for-testing/
# https://developer.chrome.com/docs/chromedriver/downloads?hl=zh-cn
# service = ChromeService(executable_path="./chromedriver")
# driver = webdriver.Chrome(service=service)

# 隐式等待2s，全局设置，适用于每一个元素定位调用
driver.implicitly_wait(2)
# 3. 操作浏览器对象的行为
# 让浏览器访问网页
driver.get("https://www.baidu.com/")
# 获取输入框元素
input_element = driver.find_element(by=By.ID, value='kw')
# 输入内容
input_element.send_keys('bilibili')
button_element = driver.find_element(by=By.ID, value='su')
button_element.click()
url_element = driver.find_element(by=By.XPATH, value='//*[@id="1"]/div/h3/a[1]')
url_element.click()
time.sleep(5)


# 4. 退出浏览器
driver.quit()
